Workflow Creation with AWS Step Functions
Step Functions
Step Function is a serverless orchestration service that allows you to model workflows as state machines. Step Functions lets you coordinate multiple Lambda functions and create a workflow that are easy to change and debug. Workflow is written in JSON. JSON allows you to get the execution and visualisation of the workflow and as well as the history of the workflow.
All the work of Step function state machine is done by Tasks. Task uses an activity, a Lambda function, or by passing parameters to the API to perform the task. Task State can invoke a Lambda function, run an AWS Batch job or ECS task, Insert an item from DynamoDB, publish message to SNS or SQS.
States of Step Functions
Choice State - Choice state is used for testing a condition to send to a branch. It adds branching logic to the state machine.
Fail or Success State - It stops the execution of the step function and marks it as failure or success.
Wait State - It gives delay for a certain amount of time.
Pass State - passes its input to its output without performing task.
Map State - used for iterating the steps dynamically.
Parallel State - used for creating parallel branches of execution.
Creating Workflow with AWS Step Functions and AWS Lambda
Step 1 : Lambda Function creation
Open AWS Lambda console, click on create function and choose Author from Scratch option. Enter the name of the function and runtime as Node.js 14.x. Choose create new role and click create function.
Add the below code in index.js file in Lambda Function. This code is executed when the Step Function executes.
exports.handler = (event, context, callback) => { callback(null, "Appointment " + event.astatus ); };
Step 2 : Creating State Machine
Open AWS Step Functions Console, click on create State machine. Select "write your workflow in code" option and Type as standard.
Replace the code snippet with the below code. The code defines the workflow for appointment status check. Copy the Lambda Function ARN and paste it in "Resource" field of this code. The code checks for the appointment status and displays the results accordingly.
{ "Comment": "Checking the appointment status", "StartAt": "Book Appointment", "States": { "Book Appointment": { "Type": "Task", "Resource": "Lambda Function ARN", "InputPath": "$", "Next": "Check Appointment status" }, "Check Appointment status": { "Type": "Choice", "Choices": [ { "Variable": "$", "StringMatches": "*accepted*", "Next": "Accepted" } ], "Default": "Rejected" }, "Accepted": { "Type": "Pass", "Result": "Appointment accepted", "End": true }, "Rejected": { "Type": "Fail", "Error": "Appointment Rejected", "Cause": "The appointment status is found rejected" } } }
The below image shows the visualisation of the workflow.
Click Next. Enter the name of the state machine, select create new role and Click Create State Machine. This will create a new state machine.
Step 3 : Execution of the State Machine
Select the State Machine created, click start execution. Provide the variables and the values in the Input dialog box and start the execution.
Execution for State type Pass - In this code the execution ends successfully if the status variable is set to "accepted". This green part of the below image shows the successful execution of the workflow. Click Input and Output in Execution details to view the results of workflow.
Execution for State type Fail - The execution ends with failure, if the status variable is set to "rejected". Red colour displays the Fail State.
When the workflow starts the execution , It invokes the Lambda Function. The Step output shows the output of the lambda function.
Conclusion
Serverless Workflow was successfully created using AWS Step Function and AWS Lambda for Checking Appointment Status. Step Function also provides visual workflow interface, where the workflow can be created with drag and drop. Complex business Logics can be created as event-driven workflows. Step Functions requires less code to be written for integration with other services.